Ext.namespace("DeptPlan");

DeptPlan.Search2 = function() {
	this.winView = null;
	this.winMsg=null;
	this.remind=null;
	this.storeYear=new Ext.data.ArrayStore({
	        	fields: ['value', 'name'],
	        	data: OMS.DEPT_YEAR
	    });	
	this.cbYear = new Ext.form.ComboBox({
		width: 100,
		name:'planYear',
		store:this.storeYear ,
		valueField: 'value',
        displayField: 'name',
        editable: false,
        mode: 'local',
        triggerAction: 'all',
        selectOnFocus: true
	});
	this.storeYear.insert(0,new this.storeYear.recordType({value:'',name:'全部'}, 101));
	this.storeMonth= new Ext.data.ArrayStore({
	        	fields: ['value', 'name'],
	        	data: OMS.DEPT_MONTH
	    });
	this.cbMonth = new Ext.form.ComboBox({
		width: 100,
		name:'planMonth',
		store:this.storeMonth,
		valueField: 'value',
        displayField: 'name',
        editable: false,
        mode: 'local',
        triggerAction: 'all',
        selectOnFocus: true
	});
	this.storeMonth.insert(0,new this.storeMonth.recordType({value:'',name:'全部'}, 101));
	this.storeParBranch=new Ext.data.JsonStore({
	    autoDestroy: true,
	    root:'result',
	    url: '../branch/getRegions.form?timesnap=' + Date.parse(new Date()),
	    fields: ['BRANCH_ID', 'BRANCH_NAME' ]
	});
	this.storeParBranch.load();
	this.cbParBranch = new Ext.form.ComboBox({
		width: 100,
		name:'parBranchId',
		store:this.storeParBranch,
        valueField: 'BRANCH_ID',
        displayField: 'BRANCH_NAME',
        editable: false,
        mode: 'local',
        triggerAction: 'all',
        selectOnFocus: true,
        listeners:{
	         scope: this,
	         'select': this.changePar
	    }        
	});
	this.storeParBranch.insert(0,new this.storeParBranch.recordType({BRANCH_ID:'',BRANCH_NAME:'全部'}, 102));
    this.storeBranch=new Ext.data.JsonStore({
	    autoDestroy: true,
	    root:'result',
	    url: '../branch/getDepts.form?timesnap=' + Date.parse(new Date()),
	    fields: ['BRANCH_ID', 'BRANCH_NAME' ]
	});
	this.cbBranch = new Ext.form.ComboBox({
		width: 100,
		name:'branchId',
		store:this.storeBranch,
        valueField: 'BRANCH_ID',
        displayField: 'BRANCH_NAME',
        editable: false,
        mode: 'local',
        triggerAction: 'all',
        selectOnFocus: true
	});
	
    this.btnSearch2 = new Ext.Toolbar.Button({
        iconCls: 'icon-search',
        text: '查找',
        tooltip: {text: '查找计划小结'},
        handler: this.queryDeptPlan,
        scope: this
    });
		
	this.store = new Ext.data.JsonStore({
	    url: '../deptPlanQuery/searchManager.form?timesnap=' + Date.parse(new Date()),
	    root: 'result',
	    totalProperty: 'total',
        remoteSort: true,
        fields: ['managerId','planYear','planMonth','branchId','parBranchId',
        'planTitle','planContent','createId','submitTime','createTime','startDate',
        'endDate','managerType','managerState','yearMonth',
        'parBranchName','branchName','createName','files']
	});
	this.renderStatus = function(value){
		return OMS.DEPT_STATE_VALUE[value];
	};
	this.renderFiles = function(value){
		var result='';
		if(value){			
			for(var i=0;i<value.length;i++){
				var href='../deptPlanManager/downloadFile.form?fileId='+value[i].ACCESSORY_ID;
				result+='[<a href="'+href+'">'+value[i].ACCESSORY_NAME+'</>]';
			}
		}
		return result;
	};
	this.column = [
		{header: 'ID', sortable: true, align: 'right',width: 20,hidden:true,dataIndex: 'managerId'},
		{header: '年度', sortable: true, align: 'center',width: 20,dataIndex: 'planYear'},
		{header: '月份', sortable: true, align: 'center',width: 20,dataIndex: 'planMonth'},
		{header: '区域', sortable: true, align: 'center',width: 20,dataIndex: 'parBranchName'},
		{header: '营业厅', sortable: true, align: 'center',width: 20,dataIndex: 'branchName'},
		{header: '营业厅ID', sortable: true, align: 'center',width: 20,hidden:true,dataIndex: 'branchId'},
		{header: '区域ID', sortable: true, align: 'center',width: 20,hidden:true,dataIndex: 'parBranchId'},
		{header: '名称', sortable: true, align: 'center',width: 20,hidden:true,dataIndex: 'planTitle'},
		{header: '内容', sortable: true, align: 'center',width: 20,hidden:true,dataIndex: 'planContent'},
		{header: '创建人', sortable: true, align: 'center',width: 20,hidden:true,dataIndex: 'createId'},
		{header: '提交时间', sortable: true, align: 'right',width: 20,hidden:true,dataIndex: 'submitTime'},
		{header: '创建时间', sortable: true, align: 'right',width: 20,hidden:true,dataIndex: 'createTime'},
		{header: '开始日期', sortable: true, align: 'right',width: 20,hidden:true,dataIndex: 'startDate'},
		{header: '截至日期', sortable: true, align: 'right',width: 20,hidden:true,dataIndex: 'endDate'},
		{header: '类型', sortable: true, align: 'center',width: 20,hidden:true,dataIndex: 'managerType'},
		{header: '状态', sortable: true, align: 'center',width: 20,dataIndex: 'managerState',renderer:this.renderStatus},
		{header: '年月', sortable: true, align: 'center',width: 20,hidden:true,dataIndex: 'yearMonth'},
		{header: '附件', align: 'center',dataIndex: 'files',renderer:this.renderFiles}
	];
			
	DeptPlan.Search2.superclass.constructor.call(this, {
		title: '查询计划小结',
        region: 'center',
        cm: new Ext.grid.ColumnModel(this.column),
        sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
        tbar: ['->','选择时间:', this.cbYear, this.cbMonth,
        '选择区域:',this.cbParBranch,'选择营业厅：',this.cbBranch,this.btnSearch2],
        bbar:new Ext.PagingToolbar({
			 pageSize: 15,
	         store: this.store,
	         displayInfo: true,
	         displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
	         emptyMsg: "没有记录..."
		}),
        store:this.store,
        viewConfig: { forceFit: true },
        listeners:{
        	rowdblclick : this.onView,
			//render:this.queryDeptPlan
			activate: this.queryDeptPlan
		}
    });
};
Ext.extend(DeptPlan.Search2, Ext.grid.GridPanel, {
	queryDeptPlan:function(btn,event){
		var params={
			start : 0,
			limit : 15,
			planYear:this.cbYear.getValue(),
			planMonth:this.cbMonth.getValue(),
			parBranchId:this.cbParBranch.getValue(),
			branchId:this.cbBranch.getValue()
		};
		this.store.load({
			params:params
		});
	},
	changePar:function(combo,record,index){
		var value=this.cbParBranch.getValue();
		this.cbBranch.reset();
		if(value==''){
			this.cbBranch.removeAll();
		}else{
			this.storeBranch.load({
				params:{branchId:value}
			});
		}
		this.storeBranch.insert(0,new this.storeBranch.recordType({BRANCH_ID:'',BRANCH_NAME:'全部'}, 10222));
	},
	onView:function(grid, rowIndex,e){		
		if (this.winView == null) {
			this.winView = new DeptPlan.View(this);
		}
		this.winView.show();
		this.winView.initWin(grid.store.getAt(rowIndex).data);	
	}
});
